package hn.cch.retry.service.impl;

import hn.cch.retry.service.RetryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class RetryServiceImpl implements RetryService {

    private final static Logger logger = LoggerFactory.getLogger(RetryServiceImpl.class);

    @Retryable(
        include = {ArithmeticException.class},
        exclude = {NullPointerException.class},
        maxAttempts = 3,
        backoff = @Backoff(delay = 3 * 1000L, multiplier = 2)
    )
    public void retryMethod() {
        logger.info("retry start");
        double a = 1 / 0;
        logger.info("retry end");
    }


    @Retryable(
        include = {ArithmeticException.class},
        exclude = {NullPointerException.class},
        maxAttempts = 3,
        backoff = @Backoff(delay = 3 * 1000L, multiplier = 2)
    )
    public void retryMethod1(String str) {
        logger.info("retry str:{} start", str);
        double a = 1 / 0;
        logger.info("retry str:{} end", str);
    }


    @Recover
    public void recoverMethod(Throwable throwable) {
        logger.info("execute more times,throwable:{}", throwable.getMessage());
    }

    @Recover
    public void recoverMethod1(Throwable throwable, String str) {
        logger.info("execute more times, str:{}，throwable:{}", str, throwable.getMessage());
    }


}
